/*
 * Created on 03.11.2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nsu.konus.control;

import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.channels.ClosedChannelException;
import java.util.ArrayList;
import java.util.List;

import com.nsu.konus.control.commands.Command;
import com.nsu.konus.control.commands.ControlChars;
import com.nsu.konus.control.commands.SpecialCommand;
import com.nsu.konus.control.commands.StringCommand;

/**
 * @author Dragon
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CommandsSender {
	
	private LocalServer server;
	
	private List commandList;
	
	private List specialCommandList;
	
	public CommandsSender(int port) {
		server = new LocalServer(port);
		commandList = new ArrayList();
		specialCommandList = new ArrayList();
		initCommandsList();
	}
	
	private void initCommandsList() {
		commandList.add(new Command("enter",
				ControlChars.ENTER, Command.KEY_DOWN_UP));
		commandList.add(new Command("left",
				ControlChars.LEFT, Command.KEY_DOWN_UP));
		commandList.add(new Command("right",
				ControlChars.RIGHT, Command.KEY_DOWN_UP));
		commandList.add(new Command("down",
				ControlChars.DOWN, Command.KEY_DOWN_UP));
		commandList.add(new Command("up",
				ControlChars.UP, Command.KEY_DOWN_UP));
		commandList.add(new Command("esc",
				ControlChars.ESCAPE, Command.KEY_DOWN_UP));
		commandList.add(new Command("backspace",
				ControlChars.BACKSPACE, Command.KEY_DOWN_UP));
		commandList.add(new Command("f1",
				ControlChars.F1, Command.KEY_DOWN_UP));
		commandList.add(new Command("f10",
				ControlChars.F10, Command.KEY_DOWN_UP));
		
		specialCommandList.add(new StringCommand("!"));
	}
	
	public void start() throws UnknownHostException, IOException {
		server.initialize();
		server.listen();
	}
	
	public void findDosboxClient() throws ClosedChannelException, IOException {
		server.accept();
	}
	
	public void sendCommand(String commandName) throws IOException, InterruptedException {
		int size = commandList.size();
		for(int i = 0; i < size; i++) {
			Command command = (Command) commandList.get(i);
			if (commandName.equalsIgnoreCase(command.getName())) {
				server.write(new byte[] {(byte)command.getAction(), (byte)command.getKey()});
				return;
			}
		}
		size = specialCommandList.size();
		for(int i = 0; i < size; i++) {
			SpecialCommand command = (SpecialCommand) specialCommandList.get(i);
			String prefix = command.getPrefix();
			if (commandName.startsWith(prefix)) {
				int len = prefix.length();
				byte[] commandNameInBytes = commandName.getBytes();
				String postfix = new String (
						commandNameInBytes, len, commandNameInBytes.length - 1);
				Command[] commands = command.getCommands(postfix);
				for(int j = 0; j < commands.length; j++) {
					byte[] bytes = new byte[] {(byte)commands[j].getAction(), 
							(byte)commands[j].getKey()};
					while(!server.write(bytes));
					//It shouldn't be in a moment
					Thread.sleep(5);
				}
				return;
			}
		}
	}
	
	public String readCommand() throws IOException {
		byte[] bytes = server.read();
		if (bytes != null)
			return new String(bytes);
		return null;
	}

}
